using System;
using System.Globalization;

namespace OpenWaves.ActivityFeed.Web
{
    public class PaginationRequest
    {
        public const int DefaultPageSize = 10;

        public DateTime? DateFrom { get; set; }
        public int PageSize { get; set; }

        public static PaginationRequest FromQuery(UrlQueryCollection parameters)
        {
            DateTime? dateFromFilter = null;
            DateTime dateFrom;
            if (parameters.ContainsKey("dateFrom") && DateTime.TryParse(parameters["dateFrom"], CultureInfo.InvariantCulture, DateTimeStyles.AdjustToUniversal, out dateFrom))
            {
                dateFromFilter = dateFrom.AddSeconds(-1);
            }
            int? pageSize = null;
            int pageSizeParam;
            if (parameters.ContainsKey("pageSize") && int.TryParse(parameters["pageSize"], out pageSizeParam))
            {
                pageSize = pageSizeParam;
            }
            else
            {
                pageSize = DefaultPageSize;
            }

            return new PaginationRequest() { DateFrom = dateFromFilter, PageSize = pageSize.Value };
        }
    }
}